package com.zslibrary.Protocol;

import com.zslibrary.Enum.PipeResultDefine;
import com.zslibrary.Enum.Protocol;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Structure.DefaultAdjustmentStruct;
import com.zslibrary.Structure.GeneralRequestStruct;
import com.zslibrary.Structure.ReplyStruct;
import com.zslibrary.Structure.UserAdjustmentStruct;
import com.zslibrary.Tools.FrameBuilder;
import com.zslibrary.Tools.ZSLog;

import java.util.List;

public class Adjustment {
    public static @StateEventDefine.StateEvent int setDefaultAdjustment(Pipe pipe){
        GeneralRequestStruct request=new GeneralRequestStruct();
        List<DefaultAdjustmentStruct> defaultAdj= Machine.getDefaultAdjustments();
        int ajdSize=defaultAdj.size();
        if(defaultAdj.size()==0)
            return StateEventDefine.EVT_NONE;
        short data_len=(short)(9*ajdSize+2);
        FrameBuilder dataBuilder=new FrameBuilder(data_len);
        dataBuilder.append((short)ajdSize);
        for(int i=0;i<ajdSize;i++){
            DefaultAdjustmentStruct adj=defaultAdj.get(i);
            dataBuilder.append(adj.productId);
            dataBuilder.append(adj.componentId);
            dataBuilder.append(adj.fluid);
            dataBuilder.append(adj.adjustmentId);
            dataBuilder.append(adj.scheme);
            dataBuilder.append(adj.value);
        }
        request.requstId= Protocol.MSG_SET_DEFAULT_ADJS;
        request.dataLength=data_len;
        request.data=dataBuilder.toBytes();
        int result=pipe.synchronousPipe(request,null);
        if(result== PipeResultDefine.SYNC_BUSY)
            return StateEventDefine.EVT_EXECUTE;
        if(result==PipeResultDefine.SYNC_ERROR)
            return StateEventDefine.EVT_DISCONNECT;
        return StateEventDefine.EVT_NONE;
    }

    public static @StateEventDefine.StateEvent int setUserAdjustment(Pipe pipe){
        List<UserAdjustmentStruct> userAdj= Adjustments.getUserAdjustments();
        if(userAdj==null){
            return StateEventDefine.EVT_NONE;
        }
        ZSLog.d("set adjustment");
        GeneralRequestStruct request=new GeneralRequestStruct();
        short data_len=(short)(8*userAdj.size()+2);
        FrameBuilder dataBuilder=new FrameBuilder(data_len);
        dataBuilder.append((short)userAdj.size());
        for(UserAdjustmentStruct adj:userAdj){
            dataBuilder.append(adj.productId);
            dataBuilder.append(adj.componentId);
            dataBuilder.append(adj.fluid);
            dataBuilder.append(adj.adjustmentId);
            dataBuilder.append(adj.value);
        }
        request.requstId=Protocol.MSG_SET_USER_ADJS;
        request.dataLength=data_len;
        request.data=dataBuilder.toBytes();
        int result=pipe.synchronousPipe(request,null);
        if(result==PipeResultDefine.SYNC_BUSY)
            return StateEventDefine.EVT_EXECUTE;
        if(result==PipeResultDefine.SYNC_ERROR)
            return StateEventDefine.EVT_DISCONNECT;
        return StateEventDefine.EVT_NONE;
    }

    public static @StateEventDefine.StateEvent int setAllUserAdjustment(Pipe pipe,UserAdjustmentStruct[] userAdjustment){
        Adjustments.setAllUserAdjustment(userAdjustment);
        if((userAdjustment!=null)&&(userAdjustment.length>0)){
            return setUserAdjustment(pipe);
        }
        return StateEventDefine.EVT_NONE;
    }

    public static @StateEventDefine.StateEvent int addUserAdjustment(Pipe pipe, UserAdjustmentStruct userAdjustment){
        Adjustments.addUserAdjustment(userAdjustment);
        return setUserAdjustment(pipe);
    }
}
